<html>
<head><meta charset="utf-8"><title>TBAA Like Type Information · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html">TBAA Like Type Information</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="214925190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/214925190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tiberio Ferreira <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#214925190">(Oct 29 2020 at 04:23)</a>:</h4>
<p>Hello everyone <span aria-label="hello" class="emoji emoji-1f44b" role="img" title="hello">:hello:</span> I'm talking with the author of <a href="https://internals.rust-lang.org/t/automatic-differentiation-differential-programming-via-llvm/13188/7">https://internals.rust-lang.org/t/automatic-differentiation-differential-programming-via-llvm/13188/7</a> and even though most of the types can be inferred from LLVM IR itself, sometimes it can't, such as: </p>
<p><code>store i128 36893488147419103234, i128* %_34.sroa.0.0..sroa_cast.i, align 8, !alias.scope !152, !noalias !151</code></p>
<p>It could be storing a double represented by that number or an integer, or anything really and the type changes the generated derivative code. C/C++ and Julia get around this issue with the TBAA information, which Enzyme uses to know what is what. Is there some similar mechanism I could use to pass Enzyme this info?</p>



<a name="214930561"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/214930561" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#214930561">(Oct 29 2020 at 06:29)</a>:</h4>
<p>I know passing through C/C++-style TBAA would be a (IMO breaking) change to rust's memory model, and while I don't work on the compiler it would be very unfortunate to make this sort of change, since Rust knows what things can alias in other (more useful) ways, and the lack of "typed memory" that TBAA generally implies makes unsafe code much easier to write (once you get used to it).</p>



<a name="214948771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/214948771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#214948771">(Oct 29 2020 at 10:25)</a>:</h4>
<p>Yeah it would be hopelessly breaking to try and give Rust TBAA at this point. It would be Rust 2.0 in a big drastic way to all sorts of unsafe code everywhere.</p>



<a name="214949380"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/214949380" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#214949380">(Oct 29 2020 at 10:32)</a>:</h4>
<p>Looking at the discussion context, it seems that what is actually needed is typed loads/stores, not TBAA specifically, and AFAIK rust can provide that information</p>



<a name="214967122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/214967122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tiberio Ferreira <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#214967122">(Oct 29 2020 at 13:40)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information/near/214949380">said</a>:</p>
<blockquote>
<p>Looking at the discussion context, it seems that what is actually needed is typed loads/stores, not TBAA specifically, and AFAIK rust can provide that information</p>
</blockquote>
<p>Yep, that is exactly what I am hoping for. <span aria-label="smiley" class="emoji emoji-1f603" role="img" title="smiley">:smiley:</span>  So far I haven't found a way to get the "real" types of the load/stores.</p>



<a name="214989143"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/214989143" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#214989143">(Oct 29 2020 at 16:06)</a>:</h4>
<p>what happens if a store happens with one type and a load with another? this is legal in rust.</p>
<p>possibly the ideal would be that such a function probably just "wouldn't work" in some sense with automatic differentiation. i've never used auto-differentiation tooling though, so i don't know how much it's supposed to work with arbitrary code.</p>



<a name="214992706"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/214992706" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tiberio Ferreira <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#214992706">(Oct 29 2020 at 16:31)</a>:</h4>
<p>Hm.. like a cast? Autodiff handles casts, so I don't think that would be a problem.</p>



<a name="214993117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/214993117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tiberio Ferreira <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#214993117">(Oct 29 2020 at 16:33)</a>:</h4>
<p>Maybe MIR has that type info, but I don't think MIR to LLVM is close to 1:1, so no luck there.</p>



<a name="214993220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/214993220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#214993220">(Oct 29 2020 at 16:34)</a>:</h4>
<p><span class="user-mention silent" data-user-id="219852">Tiberio Ferreira</span> <a href="#narrow/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information/near/214992706">said</a>:</p>
<blockquote>
<p>Hm.. like a cast? Autodiff handles casts, so I don't think that would be a problem.</p>
</blockquote>
<p>like a transmute</p>



<a name="214993238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/214993238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#214993238">(Oct 29 2020 at 16:34)</a>:</h4>
<p>when you store something as an int then load it as a float</p>



<a name="214994019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/214994019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#214994019">(Oct 29 2020 at 16:40)</a>:</h4>
<p>yeah, the closest cast-like thing to what I'm talking about is <code>std::bit_cast</code> in c++ which is still weaker than transmute in various ways.</p>



<a name="214994449"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/214994449" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#214994449">(Oct 29 2020 at 16:44)</a>:</h4>
<p>transmute is <code>reinterpret_cast</code> I thought?</p>



<a name="214994500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/214994500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tiberio Ferreira <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#214994500">(Oct 29 2020 at 16:44)</a>:</h4>
<p>That is also valid in C/C++ and Julia and is handled without a problem (when Enzyme knows the types using the TBAA table). So I think it is not a big problem.</p>



<a name="214994591"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/214994591" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tiberio Ferreira <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#214994591">(Oct 29 2020 at 16:45)</a>:</h4>
<p>Well, in C/C++ you can have any type -&gt; void* and void* -&gt; any type. I don't understand how that is different than from a transmute.</p>



<a name="214995183"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/214995183" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tiberio Ferreira <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#214995183">(Oct 29 2020 at 16:50)</a>:</h4>
<p>According to <a href="https://doc.rust-lang.org/std/mem/fn.transmute.html">https://doc.rust-lang.org/std/mem/fn.transmute.html</a> <code>transmute is semantically equivalent to a bitwise move of one type into another. It copies the bits from the source value into the destination value, then forgets the original. It's equivalent to C's memcpy under the hood, just like transmute_copy.</code></p>



<a name="215000372"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215000372" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215000372">(Oct 29 2020 at 17:25)</a>:</h4>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> no, it's similar but using reinterpret_cast like transmute (often) leads to UB (it also only works as a transmute when used on pointers).</p>
<p><span class="user-mention" data-user-id="219852">@Tiberio Ferreira</span> again, you can go through void, but doing so in that way leads to UB in c/c++ because it violates strict aliasing.</p>



<a name="215005188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215005188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215005188">(Oct 29 2020 at 17:59)</a>:</h4>
<blockquote>
<p>transmute is <code>reinterpret_cast</code> I thought?</p>
</blockquote>
<p>In this issue comment, I mention transmute is not like reinterpret_cast: <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/253#issuecomment-715909743">https://github.com/rust-lang/unsafe-code-guidelines/issues/253#issuecomment-715909743</a><br>
The TL;DR of the background is that C++ permits pointers of different types to be represented differently, so a reinterpret_cast does not have to be a bitwise no-op.</p>



<a name="215049819"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215049819" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215049819">(Oct 30 2020 at 02:07)</a>:</h4>
<p>Oh, more different than just how <code>static_cast&lt;T*&gt;</code> and <code>reinterpret_cast&lt;T*&gt;</code> can do different things because the former accounts for subobject offsets?</p>



<a name="215053528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215053528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215053528">(Oct 30 2020 at 03:29)</a>:</h4>
<p>right, I was aware that c++'s casts can get surprisingly involved in the case of virtual and multiple inheritance. But in general even for POD types on architectures where pointers are normal, reinterpret_cast isn't transmute</p>



<a name="215115661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215115661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215115661">(Oct 30 2020 at 16:08)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="120791">@RalfJ</span></p>



<a name="215130404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215130404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215130404">(Oct 30 2020 at 18:04)</a>:</h4>
<p><span class="user-mention silent" data-user-id="219852">Tiberio Ferreira</span> <a href="#narrow/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information/near/214967122">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information/near/214949380">said</a>:</p>
<blockquote>
<p>Looking at the discussion context, it seems that what is actually needed is typed loads/stores, not TBAA specifically, and AFAIK rust can provide that information</p>
</blockquote>
<p>Yep, that is exactly what I am hoping for. <span aria-label="smiley" class="emoji emoji-1f603" role="img" title="smiley">:smiley:</span>  So far I haven't found a way to get the "real" types of the load/stores.</p>
</blockquote>
<p>In Rust, typically memory <em>accesses</em> are typed, but memory itself has no notion of "values" or "types", just a bunch of raw bytes</p>



<a name="215130505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215130505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215130505">(Oct 30 2020 at 18:05)</a>:</h4>
<p>there are some exceptions though, like <code>copy_nonoverlapping</code>, where there is no reliable type information and the raw bytes are shuffled around directly</p>



<a name="215130537"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215130537" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215130537">(Oct 30 2020 at 18:05)</a>:</h4>
<p>(but that can equivalently be seen as doing the access at type <code>[MaybeUninit&lt;u8&gt;]</code>)</p>



<a name="215130686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215130686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215130686">(Oct 30 2020 at 18:06)</a>:</h4>
<p>but if you support C, you have to support type punning through unions or <code>char*</code>; Rust's transmute are no more complicated than that ;)</p>



<a name="215130729"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215130729" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215130729">(Oct 30 2020 at 18:07)</a>:</h4>
<p>(C++ might have way stranger rules for pointers than I expected, as <span class="user-mention" data-user-id="257758">@Connor Horman</span> pointed out, but I'd be surprised if anything like that also applies to C. Also I assume the vast majority of C++ programs out there will cease to work under such exotic implementations where different pointer types are represented differently.^^ But that is a separate concern. My main takeway from this is to always use C and never C++ for my examples. ;)</p>



<a name="215139119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215139119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215139119">(Oct 30 2020 at 19:17)</a>:</h4>
<blockquote>
<p>you have to support type punning through unions or <code>char*</code></p>
</blockquote>
<p>Aside: worth noting that the set of type punning that's allowed in C via a union is fairly restricted, although most compilers support a wider range of this as an extension to the standard.</p>
<p>there also was a lot of debate at one point on whether even type punning with <code>memcpy</code> was valid in c++ — note that memcpy <em>explicitly preserves</em> the "effective type" of its source. That said I don't think C (sans-++) has a notion of "effective type" at all, and so it's of course valid there.</p>
<p>My concern with all of this is how strictly an extension like enzyme will decide to apply rules like these to the IR</p>



<a name="215153002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215153002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215153002">(Oct 30 2020 at 21:30)</a>:</h4>
<p>Because of that ping, I was actually curious, because I believe C was just as silent on the equivalence of pointer representation.<br>
Per <a href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf">http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf</a>, §6.2.5, clause 26, it requires that <code>void*</code> and <code>char*</code> (as well as <code>(un)signed char*</code>) to all have the same representation, pointers to compatible types similarily do, as do all pointers to any structure type, and same with all pointers to any union type, within each such class. Other than that, it outright says they do not have to be equivalent. <br>
I'd argue that because of strict-aliasing and the one-definition-rule (or C's equivalent), the difference in representation is never measureable (well, technically, unions could allow you to observe this).</p>
<p>I'd add that TBAA extends further than simply strict-aliasing analysis, for example, in showing that two pointers do not overlap because they are the same type (see my question in <a class="stream" data-stream-id="136281" href="/#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines">#t-lang/wg-unsafe-code-guidelines</a> on the <code>UnsafeCell&lt;T&gt;</code>). However, I am not aware of how useful this would be in llvm, in particular, since the codegen has already reified type information to determine whether or not to emit certain types of optimizations.</p>



<a name="215189142"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215189142" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215189142">(Oct 31 2020 at 12:20)</a>:</h4>
<blockquote>
<p>Aside: worth noting that the set of type punning that's allowed in C via a union is fairly restricted, although most compilers support a wider range of this as an extension to the standard.</p>
</blockquote>
<p>AFAIK the main restriction is this weird "the union must be in scope" note? Otherwise I think it is as powerful as <code>memcpy</code>-based type punning.<br>
But even if not, having <em>one</em> way to do universal type punning is sufficient to make my point. ;)</p>



<a name="215189157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215189157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215189157">(Oct 31 2020 at 12:21)</a>:</h4>
<p><span class="user-mention" data-user-id="257758">@Connor Horman</span> </p>
<blockquote>
<p>as do all pointers to any structure type</p>
</blockquote>
<p>as in, given two structs <code>S1</code> and <code>S2</code>, do all pointers to <code>S1</code> have to be represented equally, or all pointers to <em>either</em> struct?<br>
But also, you keep bringing up <em>pointer representations</em>, which only seems relevant when transmuting from one pointer type to another. But most transmutes we are discussing here are not of that sort, and for those the representation of pointers seems entirely irrelevant.</p>



<a name="215189164"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215189164" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215189164">(Oct 31 2020 at 12:21)</a>:</h4>
<p>also, what percentage of programs out there actually complies with this most strict reading of the standard? I estimate it to be 0%.^^</p>



<a name="215189215"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215189215" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215189215">(Oct 31 2020 at 12:22)</a>:</h4>
<blockquote>
<p>there also was a lot of debate at one point on whether even type punning with memcpy was valid in c++ — note that memcpy explicitly preserves the "effective type" of its source. That said I don't think C (sans-++) has a notion of "effective type" at all, and so it's of course valid there.</p>
</blockquote>
<p>wasn't memcpy supposed to be the way that you can turn your byte buffer that you got from the file system, into a struct that you defined? sure C supports <em>some</em> way to do this, this is an absolute must-have for a systems language...</p>
<p>in fact there even should be a zero-copy way to do this, but AFAIK that is actually quite tricky in C (you need unions everywhere, or so?).</p>



<a name="215189346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215189346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215189346">(Oct 31 2020 at 12:26)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information/near/215189164">said</a>:</p>
<blockquote>
<p>also, what percentage of programs out there actually complies with this most strict reading of the standard? I estimate it to be 0%.^^</p>
</blockquote>
<div class="codehilite" data-code-language="C"><pre><span></span><code><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">**</span><span class="n">argv</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p>Now that percentage is non-zero. ;)</p>



<a name="215189627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215189627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215189627">(Oct 31 2020 at 12:33)</a>:</h4>
<p>okay let's say we round to 4 significant digits ;)</p>



<a name="215269820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215269820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215269820">(Nov 02 2020 at 00:18)</a>:</h4>
<blockquote>
<blockquote>
<p>there also was a lot of debate at one point on whether even type punning with memcpy was valid in c++ — note that memcpy explicitly preserves the "effective type" of its source. That said I don't think C (sans-++) has a notion of "effective type" at all, and so it's of course valid there.</p>
</blockquote>
<p>wasn't memcpy supposed to be the way that you can turn your byte buffer that you got from the file system, into a struct that you defined? sure C supports <em>some</em> way to do this, this is an absolute must-have for a systems language...</p>
</blockquote>
<p>C++ allows you to memcpy from an object of one type to an object of another, it copies the <em>object-representation</em>. </p>
<blockquote>
<blockquote>
<p>as do all pointers to any structure type</p>
</blockquote>
<p>as in, given two structs <code>S1</code> and <code>S2</code>, do all pointers to <code>S1</code> have to be represented equally, or all pointers to <em>either</em> struct?<br>
But also, you keep bringing up <em>pointer representations</em>, which only seems relevant when transmuting from one pointer type to another. But most transmutes we are discussing here are not of that sort, and for those the representation of pointers seems entirely irrelevant.</p>
</blockquote>
<p>All pointers to <em>either</em> struct. I'd add that this also applies to transmuting from pointer to integer and integer to pointer. transmute, in an object based memory model, would be (and in my case on lccc, is) defined to yield the valid value of the result with the same <em>object-representation</em> as the parameter (picking an unspecified one if multiple are valid values, and causing UB if there isn't such a valid value). </p>
<p>Also, IIRC you need to actually have a pointer or lvalue to the union object to do type punning (however, if you have a pointer to a field, getting one is easy). </p>
<p>I would add that, from the code I have written and/or seen, none of it relies on the representation of pointer objects. I realize this is a negligible fraction, but I doubt that there isn't a non-negligible fraction that doesn't, and in fact doubt that there is a non-negligible fraction that does.</p>



<a name="215951993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215951993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215951993">(Nov 07 2020 at 12:02)</a>:</h4>
<blockquote>
<p>transmute, in an object based memory model, would be (and in my case on lccc, is) defined to yield the valid value of the result with the same object-representation as the parameter (picking an unspecified one if multiple are valid values, and causing UB if there isn't such a valid value). </p>
</blockquote>
<p>That actually sounds very similar to what happens in (my mental model of) Rust -- except that the "object representation" is the <em>only thing</em> that exists in memory. no objects, just their representation.</p>



<a name="215952023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/215952023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#215952023">(Nov 07 2020 at 12:04)</a>:</h4>
<blockquote>
<p>I would add that, from the code I have written and/or seen, none of it relies on the representation of pointer objects. </p>
</blockquote>
<p>That's related to what I said above -- <em>you</em> were the one to bring up representations of pointer objects while we were talking about things like transmute between int and float, or int and padding, or whatever. I still don't understand why you did that.^^</p>



<a name="245274661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/245274661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chuyang Chen <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#245274661">(Jul 08 2021 at 05:51)</a>:</h4>
<p><span class="user-mention silent" data-user-id="219852">Tiberio Ferreira</span> <a href="#narrow/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information/near/214967122">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information/near/214949380">said</a>:</p>
<blockquote>
<p>Looking at the discussion context, it seems that what is actually needed is typed loads/stores, not TBAA specifically, and AFAIK rust can provide that information</p>
</blockquote>
<p>Yep, that is exactly what I am hoping for. <span aria-label="smiley" class="emoji emoji-1f603" role="img" title="smiley">:smiley:</span>  So far I haven't found a way to get the "real" types of the load/stores.</p>
</blockquote>
<p>Hi, I'm working on a GSoC project to integrate Enzyme to rust and am also trapped in the "typed load/store" problem.  Can the existing rust compiler provide such information? Otherwise I should modify rustc in my project to let it generate such infomation. But I doubt whether that will be a too big change to rustc. It will be very helpful if any compiler developer can give me some opinions.</p>



<a name="245275440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/245275440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chuyang Chen <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#245275440">(Jul 08 2021 at 06:09)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information/near/214949380">said</a>:</p>
<blockquote>
<p>Looking at the discussion context, it seems that what is actually needed is typed loads/stores, not TBAA specifically, and AFAIK rust can provide that information</p>
</blockquote>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> So you think it's OK to make rustc generate that information?</p>



<a name="245289721"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/245289721" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#245289721">(Jul 08 2021 at 09:20)</a>:</h4>
<p>I think it already has the information, although I'm not sure how accessible it is. You basically need the mir type system, which is a bit weak but is still good enough to determine the type of a place (and hence of the loads/stores to that place)</p>



<a name="245289834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/245289834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#245289834">(Jul 08 2021 at 09:21)</a>:</h4>
<p>That said, I'm not really involved in the compiler development much myself so don't take my word for it</p>



<a name="245433695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/245433695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Notification Bot <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#245433695">(Jul 09 2021 at 12:02)</a>:</h4>
<p><span class="user-mention silent" data-user-id="401848">Chuyang Chen</span> has marked this topic as resolved.</p>



<a name="245433704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/TBAA%20Like%20Type%20Information/near/245433704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Notification Bot <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information.html#245433704">(Jul 09 2021 at 12:02)</a>:</h4>
<p><span class="user-mention silent" data-user-id="401848">Chuyang Chen</span> has marked this topic as unresolved.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>